
define(['template','detal','jquery','cookie'],(template,detal)=>{
    class Header {
        constructor(){
            this. headerLoad().then(()=>{
                this.search()
                this.sumshop()
                this.loginUp()
            })
        }
        headerLoad(){
            return new Promise(resolve =>{
                $('header').load('/html/modules/header.html',resolve)
            })        
        }
        search(){
            $('#input-search').on('keyup',function(){
                $('#list-search').show()
                console.log($(this))
                // $('#list-search').css('background',"#ddd")
                $(this).on('focus',function(){
                    console.log(999)
                    $('#list-search').show()
                })
                $(this).on('blur',function(){
                    $('#list-search').hide()
                })
                console.log($(this).val())
                $.getJSON('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?',{wd:$(this).val()},resp=>{
                    console.log(resp)
                    const {s} = resp
                    console.log(s)
                    // console.log(list)
                    const html = template('cate-search',{list:s})
                    // console.log(html)
                    $('#list-search').html(html)
                        $('#list-search').on('mousedown','li',function(){
                            $('#input-search').val($(this).html())
                        })
                    $(".search").on("click",function(){
                     window.open(`https://www.baidu.com/s?wd=${$("#input-search").val()}`,'_blank'); 
                    })
                })
            })
        }
        sumshop(){
            // 计算购物车总数量
            let count = 0
            let card = localStorage.getItem('card')
            // console.log(card)
            if(card){
                card = JSON.parse(card)
                count = card.reduce((total,shop)=>{
                    return  total + shop.count
                },0)
                $('#car').html(`(${count})`)
                $('#shopbox').html((count))
            }
        }
        loginUp(){
            // 先取cookie,拿到用户名，渲染登录。切换状态
            let user = $.cookie('loginUser')
            // console.log(user)
            if(user){
               $('#unLogin').hide()
               $('#isLogin').show()
               $('#isLogin b').html(user)
            }
            $('#logout').on('click',()=>{
                if(confirm('确定退出吗?')){
                    $.cookie('loginUser','',{expires:-1})
                    $('#unLogin').show()
                    $('#isLogin').hide()
                }
            })
        }
    }
    return new Header()
})

 